#pragma once
//文件版本
#include "../comm/util.hpp"
#include "../comm/log.hpp"

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <cassert>

// 根据题目list文件，加载所有的题目信息到内存中
// model: 主要用来和数据进行交互，对外提供访问数据的接口

namespace ns_model
{
    using namespace ns_util;
    using namespace ns_log;
    struct Question
    {
        std::string number; // 题目id
        std::string title; // 题目标题
        std::string star; // 题目星级
        int cpu_limit; // cpu限制
        int mem_limit; // 内存限制
        std::string desc; // 题目描述
        std::string header;//给用户预设的代码
        std::string tail;//测试用例
    };
    const std::string questins_list = "./questions/questions.list";
    const std::string questins_path = "./questions/";
    class Model
    {
    private:
        std::unordered_map<std::string, Question> questions; // 题目id和题目内容的映射
    public:
        Model()
        {
            assert(LoadQuestionList(questins_list));
        }
        ~Model() {}
        bool LoadQuestionList(const std::string& question_list)
        {
          std::ifstream in(question_list);
            if(!in.is_open())
            {
                LOG(ERROR) << "没有找到题目列表文件" << "\n";
                return false;
            }
          std::string line;
          while(getline(in,line)) {

              std::vector<std::string> tokens;
              StringUtil::Split(line, &tokens, " ");
                if(tokens.size() != 5)
                {
                    LOG(ERROR) << "题目列表文件格式错误" << "\n";
                    return false;
                }
              Question q;
              q.number = tokens[0];
              q.title = tokens[1];
              q.star = tokens[2];
              q.cpu_limit = std::stoi(tokens[3]);
              q.mem_limit = std::stoi(tokens[4]);
              std::string path = questins_path;
              path += q.number;
              path += "/";
              FileUtile::ReadFile(path + "desc.txt", &q.desc, true);
              FileUtile::ReadFile(path + "header.cpp", &q.header, true);
              FileUtile::ReadFile(path + "tail.cpp", &q.tail, true);
              questions.insert({q.number, q});
          }
          LOG(INFO)<< "题目列表文件加载成功" << "\n";
          in.close();
          return true;
        }
        bool GetAllQuestions(std::vector<Question>* out)
        {
            if(questions.size()==0)
            {
                LOG(ERROR) << "没有题目" << "\n";
                return false;
            }
            for(auto e:questions)
            {
                out->push_back(e.second);
            }
            return true;
        }
        bool GetOneQuestion(const std::string& number, Question* question)
        {
            auto it = questions.find(number);
            if(it == questions.end())
            {
                LOG(ERROR) << "没有找到题目,题目编号：" <<number<< "\n";
                return false;
            }
            *question = it->second;
            return true;
        }

       
    };
}